/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormOrderPenjualan.java
 *
 * Created on Apr 10, 2011, 2:17:55 AM
 */

package com.kifas.sales.ui;

import com.kifas.model.xml.BarangModel;
import com.kifas.model.xml.BulanKadaluarsa;
import com.kifas.model.xml.CustomerModel;
import com.kifas.model.xml.GudangModel;
import com.kifas.model.xml.PenjualanDetailModel;
import com.kifas.model.xml.PenjualanModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.ReferenceNumberingModel;
import com.kifas.model.xml.SalesModel;
import com.kifas.model.xml.SatuanModel;
import com.kifas.model.xml.StokOpnameModel;
import com.kifas.sales.ui.tablemodel.ComboBarangCellEditor;
import com.kifas.service.MasterService;
import com.kifas.service.PenjualanService;
import com.kifas.service.StokOpnameService;
import com.kifas.ui.util.TextComponentUtils;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;

/**
 *
 * @author dhamarsu
 */
public class FormOrderPenjualan extends javax.swing.JInternalFrame {
    private MasterService masterService = new MasterService();
    private PenjualanService penjualanService = new PenjualanService();
    private StokOpnameService stokOpnameService = new StokOpnameService();
    
    private List<CustomerModel> customers;
    private List<SalesModel> saless;
    private List<BarangModel> barangs;
    private List<PenjualanDetailModel> penjualanDetailModels;
    private List<BulanKadaluarsa> lKadaluarsa;
    private List<SatuanModel> satuans;    
    private List<GudangModel> gudangList;
    private List<StokOpnameModel> listStokOpname;

    private PenjualanModel p;
    /** Creates new form FormOrderPenjualan */
    public FormOrderPenjualan() {
        initComponents();
//        cboBarang.setVisible(false);
//        cboSatuan.setVisible(false);


        tableOrder.setAutoCreateColumnsFromModel(false);

        customers = masterService.getCustomers();

        saless = masterService.getSaless();
        
        barangs =  masterService.getBarangs();

        satuans = new ArrayList<SatuanModel>();

        gudangList = masterService.getGudangs();

        lKadaluarsa = new ArrayList<BulanKadaluarsa>();

        BindingGroup bindingGroup = new BindingGroup();

        JComboBoxBinding custBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                customers,
                cboCustomer);

        cboCustomer.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof CustomerModel) {
                   CustomerModel mec = (CustomerModel)value;
                   setText(mec.getNama());
               }
               return this;
           }
        });

        bindingGroup.addBinding(custBinding);

        JComboBoxBinding salesBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                saless,
                cboSales);

        cboSales.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof SalesModel) {
                   SalesModel mec = (SalesModel)value;
                   setText(mec.getNama());
               }
               return this;
           }
        });

        bindingGroup.addBinding(salesBinding);

        JComboBoxBinding barangBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                barangs,
                cboBarang);

        cboBarang.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof BarangModel) {
                   BarangModel mec = (BarangModel)value;
                   setText(mec.getKdBarang() + " : " + mec.getNamaBarang());
               }
               return this;
           }
        });
        bindingGroup.addBinding(barangBinding);

        JComboBoxBinding kadaluarsaBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                lKadaluarsa,
                cboKadaluarsa);

        cboKadaluarsa.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof BulanKadaluarsa) {
                   BulanKadaluarsa mec = (BulanKadaluarsa)value;
                   setText(mec.getBlth());
               }
               return this;
           }
        });
        bindingGroup.addBinding(kadaluarsaBinding);

        JComboBoxBinding gudangBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                gudangList,
                cboGudang);

        cboGudang.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof GudangModel) {
                   GudangModel mec = (GudangModel)value;
                   setText(mec.getNamaGudang());
               }
               return this;
           }
        });
        bindingGroup.addBinding(gudangBinding);

        JComboBoxBinding satuanBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                satuans,
                cboSatuan);

        cboSatuan.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof SatuanModel) {
                   SatuanModel mec = (SatuanModel)value;
                   setText(mec.getSatuan());
               }
               return this;
           }
        });

        bindingGroup.addBinding(satuanBinding);
        bindingGroup.bind();

        penjualanDetailModels = new ArrayList<PenjualanDetailModel>();
        tableOrder.setModel(new PenjualanTableModel(penjualanDetailModels));
        tableOrder.setAutoscrolls(true);

        tableOrder.getColumnModel().getColumn(1).setCellEditor(new ComboBarangCellEditor( cboBarang));
        tableOrder.getColumnModel().getColumn(3).setCellEditor(new ComboBarangCellEditor( cboKadaluarsa));
        tableOrder.getColumnModel().getColumn(0).setCellEditor(new ComboBarangCellEditor( cboGudang));
//        tableOrder.getColumnModel().getColumn(5).setCellEditor(new ComboBarangCellEditor( cboSatuan));

        tableOrder.getColumnModel().getColumn(1).setWidth(300);
        
        dtPenjualan.setDate(new Date());
    }

    private void refreshTotalLabel(){
        if(penjualanDetailModels !=null && !penjualanDetailModels.isEmpty()){
            BigDecimal total = BigDecimal.ZERO;
            BigDecimal pajak = BigDecimal.ZERO;
            BigDecimal discount = BigDecimal.ZERO;
            for (PenjualanDetailModel detailModel : penjualanDetailModels) {
                total = total.add(detailModel.getSubTotal());
                pajak = pajak.add(detailModel.getPajak());
                discount = discount.add(detailModel.getDiscount());
            }
            total = total.add(new BigDecimal(txtBiayaLain.getText()));
            txtSubTotal.setText(TextComponentUtils.formatNumber(total));
            txtPajak.setText(TextComponentUtils.formatNumber(pajak));
            txtDiscount.setText(TextComponentUtils.formatNumber(discount));
            
            txtTotal.setText(TextComponentUtils.formatNumber(total.add(pajak)));
        }else{
            txtSubTotal.setText("0");
            txtDiscount.setText("0");
            txtPajak.setText("0");
            txtTotal.setText("0");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jToolBar1 = new javax.swing.JToolBar();
        btnNew = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnBrowse = new javax.swing.JButton();
        btnFaktur = new javax.swing.JButton();
        btnDo = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNoSales = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        dtPenjualan = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        cboCustomer = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cboSales = new javax.swing.JComboBox();
        rdoPajak = new javax.swing.JRadioButton();
        rdoNonPajak = new javax.swing.JRadioButton();
        cboBarang = new javax.swing.JComboBox();
        cboSatuan = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        dtJtTempo = new com.toedter.calendar.JDateChooser();
        jLabel12 = new javax.swing.JLabel();
        dtTglKirim = new com.toedter.calendar.JDateChooser();
        cboGudang = new javax.swing.JComboBox();
        cboKadaluarsa = new javax.swing.JComboBox();
        jLabel15 = new javax.swing.JLabel();
        txtPOCust = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtAlamatKirim = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAlamat = new javax.swing.JTextArea();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableOrder = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtSubTotal = new javax.swing.JLabel();
        txtPajak = new javax.swing.JLabel();
        txtTotal = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        txtDiscount = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtKeterangan = new javax.swing.JTextArea();
        jLabel14 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        txtBiayaLain = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setTitle("Form Order Penjualan");

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N
        jToolBar1.setPreferredSize(new java.awt.Dimension(100, 30));

        btnNew.setText("New");
        btnNew.setFocusable(false);
        btnNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNew.setName("btnNew"); // NOI18N
        btnNew.setPreferredSize(new java.awt.Dimension(100, 29));
        btnNew.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        jToolBar1.add(btnNew);

        btnSave.setText("Save");
        btnSave.setEnabled(false);
        btnSave.setFocusable(false);
        btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSave.setName("btnSave"); // NOI18N
        btnSave.setPreferredSize(new java.awt.Dimension(100, 29));
        btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSave);

        btnCancel.setText("Cancel");
        btnCancel.setEnabled(false);
        btnCancel.setFocusable(false);
        btnCancel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.setPreferredSize(new java.awt.Dimension(100, 29));
        btnCancel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCancel);

        btnBrowse.setText("Browse");
        btnBrowse.setFocusable(false);
        btnBrowse.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBrowse.setName("btnBrowse"); // NOI18N
        btnBrowse.setPreferredSize(new java.awt.Dimension(100, 29));
        btnBrowse.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });
        jToolBar1.add(btnBrowse);

        btnFaktur.setText("Print Faktur");
        btnFaktur.setEnabled(false);
        btnFaktur.setFocusable(false);
        btnFaktur.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnFaktur.setName("btnFaktur"); // NOI18N
        btnFaktur.setPreferredSize(new java.awt.Dimension(100, 29));
        btnFaktur.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnFaktur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFakturActionPerformed(evt);
            }
        });
        jToolBar1.add(btnFaktur);

        btnDo.setText("Create DO");
        btnDo.setEnabled(false);
        btnDo.setFocusable(false);
        btnDo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDo.setName("btnDo"); // NOI18N
        btnDo.setPreferredSize(new java.awt.Dimension(100, 29));
        btnDo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnDo);

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("No Penjualan");
        jLabel1.setName("jLabel1"); // NOI18N

        txtNoSales.setText("AUTO");
        txtNoSales.setName("txtNoSales"); // NOI18N

        jLabel2.setText("Tangal");
        jLabel2.setName("jLabel2"); // NOI18N

        dtPenjualan.setDateFormatString("dd/MM/yyyy"); // NOI18N
        dtPenjualan.setName("dtPenjualan"); // NOI18N

        jLabel3.setText("Customer");
        jLabel3.setName("jLabel3"); // NOI18N

        cboCustomer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboCustomer.setName("cboCustomer"); // NOI18N
        cboCustomer.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboCustomerItemStateChanged(evt);
            }
        });

        jLabel4.setText("Sales");
        jLabel4.setName("jLabel4"); // NOI18N

        cboSales.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboSales.setName("cboSales"); // NOI18N

        buttonGroup1.add(rdoPajak);
        rdoPajak.setSelected(true);
        rdoPajak.setText("Pajak");
        rdoPajak.setName("rdoPajak"); // NOI18N

        buttonGroup1.add(rdoNonPajak);
        rdoNonPajak.setText("Harga Termasuk Pajak");
        rdoNonPajak.setName("rdoNonPajak"); // NOI18N

        cboBarang.setName("cboBarang"); // NOI18N
        cboBarang.setPreferredSize(new java.awt.Dimension(0, 0));

        cboSatuan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboSatuan.setName("cboSatuan"); // NOI18N
        cboSatuan.setPreferredSize(new java.awt.Dimension(0, 0));

        jLabel11.setText("Tgl Jth tempo");
        jLabel11.setName("jLabel11"); // NOI18N

        dtJtTempo.setDateFormatString("dd/MM/yyyy"); // NOI18N
        dtJtTempo.setName("dtJtTempo"); // NOI18N

        jLabel12.setText("Tgl Kirim");
        jLabel12.setName("jLabel12"); // NOI18N

        dtTglKirim.setDateFormatString("dd/MM/yyyy"); // NOI18N
        dtTglKirim.setName("dtTglKirim"); // NOI18N

        cboGudang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboGudang.setName("cboGudang"); // NOI18N
        cboGudang.setPreferredSize(new java.awt.Dimension(0, 0));

        cboKadaluarsa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboKadaluarsa.setName("cboKadaluarsa"); // NOI18N
        cboKadaluarsa.setPreferredSize(new java.awt.Dimension(0, 0));

        jLabel15.setText("No PO Cust");
        jLabel15.setName("jLabel15"); // NOI18N

        txtPOCust.setName("txtPOCust"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(dtPenjualan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtNoSales, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cboGudang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cboKadaluarsa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(49, 49, 49)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cboSales, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cboCustomer, 0, 176, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rdoPajak)
                        .addGap(18, 18, 18)
                        .addComponent(rdoNonPajak)))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(jLabel11)
                    .addComponent(jLabel15))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPOCust)
                    .addComponent(dtJtTempo, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                    .addComponent(dtTglKirim, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(66, 66, 66)
                .addComponent(cboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(143, 143, 143)
                .addComponent(cboSatuan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(dtTglKirim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dtJtTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPOCust, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(txtNoSales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cboCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel12)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(dtPenjualan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(cboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cboSatuan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(cboSales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(6, 6, 6)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                            .addComponent(cboKadaluarsa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(rdoPajak)
                                            .addGap(6, 6, 6))
                                        .addComponent(cboGudang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(9, 9, 9))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(rdoNonPajak)
                                    .addContainerGap()))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel15)
                                .addContainerGap())))))
        );

        jTabbedPane1.addTab("Order Penjualan", jPanel1);

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel5.setText("Alamat");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("Alamat Kirim");
        jLabel6.setName("jLabel6"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtAlamatKirim.setColumns(20);
        txtAlamatKirim.setRows(5);
        txtAlamatKirim.setName("txtAlamatKirim"); // NOI18N
        jScrollPane1.setViewportView(txtAlamatKirim);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        txtAlamat.setColumns(20);
        txtAlamat.setEditable(false);
        txtAlamat.setRows(5);
        txtAlamat.setName("txtAlamat"); // NOI18N
        jScrollPane2.setViewportView(txtAlamat);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(54, 54, 54))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 536, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 536, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(245, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(17, 17, 17))
        );

        jTabbedPane1.addTab("Alamat", jPanel2);

        jTabbedPane2.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        jTabbedPane2.setName("jTabbedPane2"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        tableOrder.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Gudang", "Kode BRG", "Nama", "Kadaluarsa", "Qty", "Satuan", "Harga", "Disc(%)", "Discount", "Pajak", "Sub Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                true, true, true, true, false, true, false, false, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableOrder.setName("tableOrder"); // NOI18N
        tableOrder.setRowHeight(24);
        tableOrder.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tableOrderKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(tableOrder);

        jTabbedPane2.addTab("Daftar Item", jScrollPane3);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Biaya"));
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel7.setText("Sub Total");
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText("Pajak");
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText("Discount");
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText("Total");
        jLabel10.setName("jLabel10"); // NOI18N

        txtSubTotal.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        txtSubTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtSubTotal.setText("0");
        txtSubTotal.setName("txtSubTotal"); // NOI18N

        txtPajak.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        txtPajak.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtPajak.setText("0");
        txtPajak.setName("txtPajak"); // NOI18N

        txtTotal.setFont(new java.awt.Font("DejaVu Sans", 1, 18));
        txtTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtTotal.setText("0");
        txtTotal.setName("txtTotal"); // NOI18N

        jButton2.setText("Delete");
        jButton2.setName("jButton2"); // NOI18N

        txtDiscount.setFont(new java.awt.Font("DejaVu Sans", 1, 14));
        txtDiscount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        txtDiscount.setText("0");
        txtDiscount.setName("txtDiscount"); // NOI18N

        jLabel13.setText("Keterangan");
        jLabel13.setName("jLabel13"); // NOI18N

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        txtKeterangan.setColumns(20);
        txtKeterangan.setRows(5);
        txtKeterangan.setText("-");
        txtKeterangan.setName("txtKeterangan"); // NOI18N
        jScrollPane4.setViewportView(txtKeterangan);

        jLabel14.setText("Biaya Lain");
        jLabel14.setName("jLabel14"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        txtBiayaLain.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        txtBiayaLain.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtBiayaLain.setText("0");
        txtBiayaLain.setName("txtBiayaLain"); // NOI18N
        txtBiayaLain.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBiayaLainKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(40, 40, 40)
                        .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel8)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addComponent(jLabel14)
                            .addGap(18, 18, 18)
                            .addComponent(txtBiayaLain))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel9)
                                .addComponent(jLabel7))
                            .addGap(31, 31, 31)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtPajak, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtSubTotal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtDiscount, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)))))
                .addGap(138, 138, 138)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel13)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(txtDiscount))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel7)
                                    .addComponent(txtSubTotal))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel8)
                                    .addComponent(txtPajak))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel14)
                                    .addComponent(txtBiayaLain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(4, 4, 4)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(txtTotal)))
                    .addComponent(jButton2))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 925, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jTabbedPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 901, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void refreshTable(){
        tableOrder.setModel(new PenjualanTableModel(penjualanDetailModels));
    }

    private void clearForm(){
        penjualanDetailModels = new ArrayList<PenjualanDetailModel>();
        txtNoSales.setText("AUTO");
        dtPenjualan.setDate(new Date());
        refreshTable();
        refreshTotalLabel();

        setEnabledButton(false);
        btnNew.setEnabled(true);
        btnBrowse.setEnabled(true);
    }

    private void setComboKadaluarsa(int row, Long idGudang){
        lKadaluarsa.clear();
        try{
            BarangModel brg = penjualanDetailModels.get(row).getBarang();
            if (brg != null)
            listStokOpname = stokOpnameService.getAvailableBarang(brg.getIdBarang());
            for (StokOpnameModel st : listStokOpname){
                 if(st.getGudang().getIdMasterGudang() == idGudang)
                 lKadaluarsa.add(new BulanKadaluarsa(st.getBulanKadaluarsa()));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void setComboGudang(int row){
        gudangList.clear();
        try{
            if (listStokOpname != null){
                String bl = penjualanDetailModels.get(row).getBulanKadaluarsa();
                for (StokOpnameModel st : listStokOpname){
                    if (st.getBulanKadaluarsa().equals(bl)){
                        gudangList.add(st.getGudang());
                    }
                }            
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:
        clearForm();
        PenjualanDetailModel detailModel = new PenjualanDetailModel();
        detailModel.setQty(0L);
        detailModel.setPrice(new BigDecimal(0));
        detailModel.setSubTotal(new BigDecimal(0));
        detailModel.setDiscount(new BigDecimal(0));
        detailModel.setPajak(new BigDecimal(0));
        detailModel.setDiscount2(new BigDecimal(0));
        penjualanDetailModels.add(detailModel);
        refreshTable();

        setEnabledButton(true);
        btnNew.setEnabled(false);
        btnDo.setEnabled(false);
        btnFaktur.setEnabled(false);
    }//GEN-LAST:event_btnNewActionPerformed

    private void tableOrderKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableOrderKeyPressed
        // TODO add your handling code here:
        if (tableOrder.getSelectedColumn() == 10 && evt.getKeyCode() == KeyEvent.VK_ENTER){
            if(!tableOrder.getValueAt(tableOrder.getSelectedRow(), 0).equals("")){
                PenjualanDetailModel detailModel = new PenjualanDetailModel();
                detailModel.setQty(0L);
                detailModel.setPrice(new BigDecimal(0));
                detailModel.setSubTotal(new BigDecimal(0));
                detailModel.setDiscount(new BigDecimal(0));
                detailModel.setPajak(new BigDecimal(0));
                detailModel.setDiscount2(new BigDecimal(0));
                penjualanDetailModels.add(detailModel);
                refreshTable();
            }else{
                JOptionPane.showMessageDialog(null, "Kode Barang untuk baris ini tidak boleh kosong", "Information", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_tableOrderKeyPressed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        refreshTotalLabel();
        PenjualanModel pm = new PenjualanModel();
        pm.setAlamatKirim(txtAlamatKirim.getText());
        pm.setCustomer((CustomerModel) cboCustomer.getSelectedItem());
        if (rdoPajak.isSelected()){
            pm.setIsPajak("1");
        }else{
            pm.setIsPajak("0");
        }
        ReferenceNumberingModel rf = masterService.getReferenceNumber(1, "PO");
        txtNoSales.setText(rf.getResult());
        pm.setNoPenjualan(txtNoSales.getText());
        pm.setSales((SalesModel) cboSales.getSelectedItem());
        pm.setTanggal(dtPenjualan.getDate());
        pm.setTotal(new BigDecimal(txtTotal.getText().replace(",", "")));
        pm.setTanggalJatuhTempo(dtJtTempo.getDate());
        pm.setTanggalKirim(dtTglKirim.getDate());
        pm.setNomorPo(txtPOCust.getText());
        pm.setBiayaLain(new BigDecimal(txtBiayaLain.getText()));
        pm.setKeterangan(txtKeterangan.getText());

        Set<PenjualanDetailModel> set = new HashSet(penjualanDetailModels);
        pm.setPenjualanDetails(set);
        try{
            PersistStatus status = penjualanService.setPenjualan(pm);
            JOptionPane.showMessageDialog(null, status.getStatus());
            if (status.getStatus().equals("success"))
                p = pm;
        }catch(Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Penyimpanan gagal.");
        }

        setEnabledButton(true);
        btnSave.setEnabled(false);
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        // TODO add your handling code here:
        p = new OrderPenjualanLookUp(null, true).getPenjualanModel();
        if (p != null){
            setEnabledButton(true);
            btnNew.setEnabled(false);
            
            txtNoSales.setText(p.getNoPenjualan());
            customers.clear();
            customers.add(p.getCustomer());
            saless.clear();
            saless.add(p.getSales());
            dtPenjualan.setDate(p.getTanggal());
            dtJtTempo.setDate(p.getTanggalJatuhTempo());
            dtTglKirim.setDate(p.getTanggalKirim());

            penjualanDetailModels = new ArrayList<PenjualanDetailModel>(p.getPenjualanDetails());
            refreshTable();

            refreshTotalLabel();
            
        }        
    }//GEN-LAST:event_btnBrowseActionPerformed

    private void cboCustomerItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCustomerItemStateChanged
        // TODO add your handling code here:
        CustomerModel cm = (CustomerModel)cboCustomer.getSelectedItem();
        if (cm != null){
            txtAlamat.setText(cm.getAlamat1());
            txtAlamatKirim.setText(cm.getAlamat1());
        }
    }//GEN-LAST:event_cboCustomerItemStateChanged

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        clearForm();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnDoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDoActionPerformed
        // TODO add your handling code here:
        if (p != null){            
            FormPengiriman formPengiriman = new FormPengiriman(p);
            this.getDesktopPane().add(formPengiriman);
            formPengiriman.setVisible(true);
        }
    }//GEN-LAST:event_btnDoActionPerformed

    private void txtBiayaLainKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBiayaLainKeyPressed
        // TODO add your handling code here:
        refreshTotalLabel();
    }//GEN-LAST:event_txtBiayaLainKeyPressed

    private void btnFakturActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFakturActionPerformed
        // TODO add your handling code here:
         try{
             List<PenjualanModel> listP = new ArrayList<PenjualanModel>();
             listP.add(p);
             Map<String, Object> parameter = new HashMap<String, Object>();
             parameter.put("SUBREPORT_DIR", "");
            JasperPrint jasperPrint = JasperFillManager.fillReport("report/rpt_faktur_penjualan.jasper"
                , parameter, new JRBeanCollectionDataSource(listP));


            JasperViewer visor = new JasperViewer(jasperPrint,false) ;
            visor.setVisible(true) ;
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnFakturActionPerformed

    private void setEnabledButton(boolean enabled){
        btnBrowse.setEnabled(enabled);
        btnDo.setEnabled(enabled);
        btnFaktur.setEnabled(enabled);
        btnNew.setEnabled(enabled);
        btnSave.setEnabled(enabled);
        btnCancel.setEnabled(enabled);
    }

    private class PenjualanTableModel extends AbstractTableModel{
        private List<PenjualanDetailModel> penjualanDetailModels;

        public PenjualanTableModel(List<PenjualanDetailModel> penjualanDetails) {
            this.penjualanDetailModels = penjualanDetails;
        }

        @Override
        public int getRowCount() {
            return penjualanDetailModels.size();
        }

        @Override
        public int getColumnCount() {
            return 5;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            PenjualanDetailModel s = penjualanDetailModels.get(rowIndex);
            try{
                if (columnIndex == 1){
                    if (s.getBarang() != null)
                        return s.getBarang().getKdBarang();
                }else if (columnIndex == 2){
                    if (s.getBarang() != null)
                        return s.getBarang().getNamaBarang();
                }else if (columnIndex == 3){
                    if (s.getBulanKadaluarsa() != null)
                        return s.getBulanKadaluarsa();
                }else if (columnIndex == 0){
                    if (s.getGudang() != null)
                        return s.getGudang().getNamaGudang();
                }else if (columnIndex == 4){
                    if (s.getQty() != null)
                        return s.getQty();
                }else if (columnIndex == 5){
                    if (s.getBarang().getSatuan().getSatuan() != null)
                        return s.getBarang().getSatuan().getSatuan();
                }else if (columnIndex == 6){
                    if (s.getPrice() != null)
                        return s.getPrice();
                }else if (columnIndex == 7){
                    if (s.getDiscount() != null){
                        return s.getDiscount();
                    }else{
                        return new BigDecimal("0");
                    }
                }else if (columnIndex == 8){
                    if (s.getDiscount2() != null){
                        return s.getDiscount2();
                    }else{
                        return new BigDecimal("0");
                    }
                }else if (columnIndex == 9){
                    if (s.getPajak() != null){
                        return s.getPajak();
                    }else{
                        return new BigDecimal("0");
                    }
                }else if (columnIndex == 10){
                        return s.getSubTotal();
                }
            }catch(Exception e){
                return "";
            }
                return "";
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if(columnIndex == 6 || columnIndex == 7 || columnIndex == 8 || columnIndex == 9 || columnIndex == 10){
                return BigDecimal.class;
            } else if(columnIndex == 4){
                return Long.class;
            }
            return String.class;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if(columnIndex == 2 || columnIndex == 10  || columnIndex == 9 || columnIndex == 8) {
                return false;
            }
            return true;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            PenjualanDetailModel s = penjualanDetailModels.get(rowIndex);
            if(columnIndex == 4){
                s.setQty((Long) aValue);
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount2()).setScale(2));
                if(rdoPajak.isSelected()){
                    s.setPajak(s.getSubTotal().multiply(new BigDecimal(0.1)));
                }else{
                    s.setPajak(new BigDecimal(0));
                }
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
            }else if (columnIndex == 1){
                s.setBarang((BarangModel) aValue);
//                satuans.clear();
//                satuans.add(s.getSatuan());
                s.setPrice(s.getBarang().getHargaJual());
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount2()).setScale(2));
                if(rdoPajak.isSelected()){
                    s.setPajak(s.getSubTotal().multiply(new BigDecimal(0.1)));
                }else{
                    s.setPajak(new BigDecimal(0));
                }
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
                setComboKadaluarsa(rowIndex, s.getGudang().getIdMasterGudang());
            }else if (columnIndex == 3){
                BulanKadaluarsa b = (BulanKadaluarsa) aValue;
                s.setBulanKadaluarsa(b.getBlth());
                setComboGudang(rowIndex);
            }else if (columnIndex == 0){
                GudangModel b = (GudangModel) aValue;
                s.setGudang(b);
            }else if (columnIndex == 5){
                s.getBarang().setSatuan((SatuanModel) aValue);
            }else if (columnIndex == 6){
                s.setPrice((BigDecimal) aValue);
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount2()).setScale(2));
                if(rdoPajak.isSelected()){
                    s.setPajak((s.getSubTotal().multiply(new BigDecimal(0.1))));
                }else{
                    s.setPajak(new BigDecimal(0));
                }
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
            }else if (columnIndex == 7){
                s.setDiscount(new BigDecimal(aValue.toString()));
                s.setDiscount2(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).multiply(s.getDiscount().divide(new BigDecimal(100))).setScale(2));
                s.setSubTotal(s.getPrice().multiply(
                    new BigDecimal(s.getQty())).subtract(s.getDiscount2()).setScale(2));
                if(rdoPajak.isSelected()){
                    s.setPajak((s.getSubTotal().multiply(new BigDecimal(0.1))));
                }else{
                    s.setPajak(new BigDecimal(0));
                }
                s.setPajak(s.getPajak().setScale(2, RoundingMode.HALF_DOWN));
            }
            refreshTable();
            refreshTotalLabel();
        }



        public void setPenjualanDetails(List<PenjualanDetailModel> penjualanDetails) {
            this.penjualanDetailModels = penjualanDetails;
        }
        

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDo;
    private javax.swing.JButton btnFaktur;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSave;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cboBarang;
    private javax.swing.JComboBox cboCustomer;
    private javax.swing.JComboBox cboGudang;
    private javax.swing.JComboBox cboKadaluarsa;
    private javax.swing.JComboBox cboSales;
    private javax.swing.JComboBox cboSatuan;
    private com.toedter.calendar.JDateChooser dtJtTempo;
    private com.toedter.calendar.JDateChooser dtPenjualan;
    private com.toedter.calendar.JDateChooser dtTglKirim;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JRadioButton rdoNonPajak;
    private javax.swing.JRadioButton rdoPajak;
    private javax.swing.JTable tableOrder;
    private javax.swing.JTextArea txtAlamat;
    private javax.swing.JTextArea txtAlamatKirim;
    private javax.swing.JTextField txtBiayaLain;
    private javax.swing.JLabel txtDiscount;
    private javax.swing.JTextArea txtKeterangan;
    private javax.swing.JTextField txtNoSales;
    private javax.swing.JTextField txtPOCust;
    private javax.swing.JLabel txtPajak;
    private javax.swing.JLabel txtSubTotal;
    private javax.swing.JLabel txtTotal;
    // End of variables declaration//GEN-END:variables
    
}
